package com.cpecc.controller.wuzi;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cpecc.framework.web.APIResult;
import com.cpecc.liucheng.manager.LiuChengManager;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.cpecc.mail.SendMailThread;
import com.cpecc.manager.system.DepManager;
import com.cpecc.manager.system.EmailManager;
import com.cpecc.liucheng.manager.HuanJieManager;
import com.cpecc.manager.system.RootManager;
import com.cpecc.manager.system.ShiYongZTManager;
import com.cpecc.manager.system.ToDoManager;
import com.cpecc.manager.system.UserManager;
import com.cpecc.manager.system.YouJianManager;
import com.cpecc.manager.wuzi.BaoFeiManager;
import com.cpecc.manager.wuzi.BuPeiManager;
import com.cpecc.manager.wuzi.HuanHuoManager;
import com.cpecc.manager.wuzi.TuiHuoManager;
import com.cpecc.manager.wuzi.WeiXiuJLManager;
import com.cpecc.manager.wuzi.WeiXiuManager;
import com.cpecc.manager.wuzi.WuZiManager;
import com.cpecc.manager.wuzi.YiJiaoJLManager;
import com.cpecc.manager.wuzi.YiJiaoManager;
import com.cpecc.model.system.Email;
import com.cpecc.model.system.ToDo;
import com.cpecc.model.system.User;
import com.cpecc.model.system.YouJian;
import com.cpecc.model.wuzi.WeiXiu;
import com.cpecc.model.wuzi.WeiXiuJL;
import com.cpecc.model.wuzi.WuZi;
import com.cpecc.model.wuzi.YiJiao;
import com.cpecc.model.wuzi.YiJiaoJL;

@Controller
@RequestMapping(value = "/wuzi")
public class WeiXiuController {

	@Autowired
	private UserManager userManager;
	
	@Autowired
	private WeiXiuManager weiXiuManager;
	
	@Autowired
	private WeiXiuJLManager weiXiuJLManager;
	
	@Autowired
	private HuanJieManager huanJieManager;
	
	@Autowired
	private WuZiManager wuZiManager;
	
	@Autowired
	private HuanHuoManager huanHuoManager;
	
	@Autowired
	private TuiHuoManager tuiHuoManager;
	
	@Autowired
	private ShiYongZTManager shiYongZTManager;
	
	@Autowired
	private DepManager depManager;
	
	@Autowired
	private EmailManager emailManager;
	
	@Autowired
	private YiJiaoManager yiJiaoManager;
	
	@Autowired
	private YiJiaoJLManager yiJiaoJLManager;
	
	@Autowired
	private BaoFeiManager baoFeiManager;
	
	@Autowired
	private BuPeiManager buPeiManager;
	
	@Autowired
	private ToDoManager toDoManager;
	
	@Autowired
	private RootManager rootManager;
	
	@Autowired
	private YouJianManager youJianManager;

	@Autowired
	private LiuChengManager liuChengManager;
	
	private List<User> getXiaYiBuR(String weiXiuId){
		List<User> list=new ArrayList<User>();
		WeiXiu weiXiu=weiXiuManager.get(weiXiuId);
		int huanJie=weiXiu.getHuanJie().getId();
		switch(huanJie){
		//维修换货
		case 204: 
		//指定维修人维修
		case 402: list.addAll(userManager.getGZGLY()); break;
		//维修人维修中
		case 403: list.add(weiXiu.getWeiXiuR()); break;
		//申请人验收
		case 404: list.add(weiXiu.getUser()); break;
		//维修费用入账
		case 405: list.addAll(userManager.getGZGLY()); break;

		case 408:
		case 409:
			list.addAll(liuChengManager.listUser(weiXiu.getLiuChengId(), huanJie));
			break;
		default:break;
		}
		return list;
	}
	
	/*
	 * 申请维修
	 */
	@RequestMapping(value = "/weiXiu")
	public ModelAndView weiXiuShenQing(String id){
		ModelAndView mav=new ModelAndView();
		mav.addObject("wuZiId", id);
		mav.setViewName("weixiu/weiXiu");
		return mav;
	}

	/**
	 * TODO 401
	 * @param wuZiId
	 * @param shenQingYY
	 * @param response
	 */
	@RequestMapping(value = "/weiXiuShenQing")
	public void weiXiuShenQing(String wuZiId,String shenQingYY,HttpServletResponse response){
		if(weiXiuManager.hasWuZi(wuZiId)){
			response.setContentType("application/json");
			String result="{\"result\":\"conflict\"}";
			try {
				response.getWriter().write(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}			
		else if(yiJiaoManager.hasWuZi(wuZiId)){
			response.setContentType("application/json");
			String result="{\"result\":\"conflict\"}";
			try {
				response.getWriter().write(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}
		else if(baoFeiManager.hasWuZi(wuZiId)){
			response.setContentType("application/json");
			String result="{\"result\":\"conflict\"}";
			try {
				response.getWriter().write(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}else if(huanHuoManager.hasWuZi(wuZiId)){
			response.setContentType("application/json");
			String result="{\"result\":\"conflict\"}";
			try {
				response.getWriter().write(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}else if(tuiHuoManager.hasWuZi(wuZiId)){
			response.setContentType("application/json");
			String result="{\"result\":\"conflict\"}";
			try {
				response.getWriter().write(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}else if(buPeiManager.hasWuZi(wuZiId)){
			response.setContentType("application/json");
			String result="{\"result\":\"conflict\"}";
			try {
				response.getWriter().write(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		WeiXiu weiXiu=new WeiXiu();
		weiXiu.setShenQingRQ(new Date());
		weiXiu.setWeiXiuYY(shenQingYY);
		//weiXiu.setHuanJie(huanJieManager.get(402));
		weiXiu.setHuanJie(huanJieManager.get(408));
		weiXiu.setUser(user);
		weiXiu.setLiuChengId(System.currentTimeMillis() + "");

		WuZi wuZi=wuZiManager.get(wuZiId);
		weiXiu.setWuZi(wuZi);
		weiXiuManager.save(weiXiu);

		liuChengManager.save(weiXiu.getLiuChengId(), 408, userManager.getBuMenLingDaos2(user));

		WeiXiuJL weiXiuJL=new WeiXiuJL();
		weiXiuJL.setCaoZuoRQ(new Date());
		weiXiuJL.setHuanJie(huanJieManager.get(401));
		weiXiuJL.setPiFuLX("使用人申请维修");
		weiXiuJL.setPiFuNR("提交");
		weiXiuJL.setUser(user);
		weiXiuJL.setWeiXiu(weiXiu);
		weiXiuJLManager.save(weiXiuJL);		
		if(yiJiaoManager.getYiJiaoByWuZi(wuZi,503)!=null){
			YiJiao yiJiao=yiJiaoManager.getYiJiaoByWuZi(wuZi,503);
			yiJiao.setHuanJie(huanJieManager.get(506));
			yiJiaoManager.cover(yiJiao);
			YiJiaoJL yiJiaoJL=new YiJiaoJL();
			yiJiaoJL.setCaoZuoRQ(new Date());
			yiJiaoJL.setHuanJie(huanJieManager.get(503));
			yiJiaoJL.setPiFuLX("库存维修");
			yiJiaoJL.setPiFuNR("维修");
			yiJiaoJL.setYiJiao(yiJiao);
			yiJiaoJL.setUser(user);
			yiJiaoJLManager.save(yiJiaoJL);
			toDoManager.deleteList(yiJiao);
		}
		String root=rootManager.getRoot();
		Email email=emailManager.getEmail();
		for(User user1:userManager.getGZGLY()){
			ToDo toDo=new ToDo();
			toDo.setCreateDate(new Date());
			toDo.setHuanJie(huanJieManager.get(402));
			toDo.setTask("维修审批");
			toDo.setType("维修");
			toDo.setUser(user1);
			toDo.setWeiXiu(weiXiu);
			toDoManager.save(toDo);
			//\\发送邮件
			YouJian youJian=youJianManager.get(402);
			if(youJian!=null && youJian.getState()==1){
				SendMailThread st=new SendMailThread(email, root, 402, user1, youJian);
				st.setWeiXiu(weiXiu);
				st.setWuZi(wuZi);
				Thread t=new Thread(st);
				t.start();
			}
		}
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * TODO DaiBan
	 * @return
	 */
	@RequestMapping(value = "/weiXiuDaiBan")
	public ModelAndView weiXiuDaiBan(){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		ModelAndView mav=new ModelAndView();
		int active=0;
		mav.addObject("weiXiuLingYong", weiXiuManager.getMyWeiXiuQR(user));
		/*
		if(weiXiuManager.getMyWeiXiuQR(user).size()>0)
			active=1;
			*/
		mav.addObject("weiXiuList", weiXiuManager.getMyWeiXiuRW(user));
		/*
		if(active==0)
			if(weiXiuManager.getMyWeiXiuRW(user).size()>0)
				active=2;
				*/

		if (currentUser.hasRole("低值管理员")) {
			mav.addObject("weiXiuShenQingList", weiXiuManager.getAllWeiXiuByZT(402));
			/*
			if(active==0)
				if(weiXiuManager.getAllWeiXiuByZT(402).size()>0)
					active=3;
					*/
			mav.addObject("weiXiuQueRenList", weiXiuManager.getAllWeiXiuByZT(405));
			/*
			if(active==0)
				if(weiXiuManager.getAllWeiXiuByZT(405).size()>0)
					active=4;
					*/
			mav.addObject("huanHuoRK", weiXiuManager.getAllWeiXiuByZT(204));
			/*
			if(active==0)
				if(weiXiuManager.getAllWeiXiuByZT(204).size()>0)
					active=5;
					*/
		}
		mav.addObject("huanHuoList", huanHuoManager.getAllHuanHuoSQ2CMR(2, user));

		mav.addObject("weiXiuList408", weiXiuManager.getMyWeiXiuRW(user, 408));
		mav.addObject("weiXiuList409", weiXiuManager.getMyWeiXiuRW(user, 409));

		mav.addObject("active", active);
		mav.setViewName("weixiu/weiXiuDaiBan");
		return mav;
	}
	
	@RequestMapping(value = "/getWeiXiuShu")
	public void getWeiXiuShu(HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		int zongShu=0;
		if(currentUser.hasRole("低值管理员")){
			zongShu+=weiXiuManager.getAllWeiXiuByZT(402).size();
			zongShu+=weiXiuManager.getAllWeiXiuByZT(405).size();
			zongShu+=weiXiuManager.getAllWeiXiuByZT(204).size();
		}
		zongShu+=huanHuoManager.getAllHuanHuoSQ2CMR(2, user).size();
		zongShu+=weiXiuManager.getMyWeiXiuQR(user).size();
		zongShu+=weiXiuManager.getMyWeiXiuRW(user).size();

		zongShu += weiXiuManager.getMyWeiXiuRW(user, 408).size();
		zongShu += weiXiuManager.getMyWeiXiuRW(user, 409).size();

		response.setContentType("application/json;charset=utf-8");		
		String result="{\"result\":\"("+zongShu+")\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * TODO WeiXiu LiuCheng
	 * @return
	 */
	@RequestMapping(value = "/weixiu/liucheng/tongguo", method = RequestMethod.POST)
	@ResponseBody
	public APIResult weiXiuLiuCheng(String weiXiuId) {
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);

		weiXiuManager.doLiuCheng(weiXiuId, user);

		return APIResult.success();
	}


	
	/*
	 * 低值管理员指定维修人
	 */
	@RequestMapping(value = "/wxspList")
	public ModelAndView wxspList(){
		ModelAndView mav=new ModelAndView();
		mav.addObject("weiXiuShenQingList", weiXiuManager.getAllWeiXiuByZT(402));
		mav.setViewName("weixiu/wxspListAjax");
		return mav;
	}
	
	@RequestMapping(value = "/weiXiuZD")
	public ModelAndView weiXiuZD(String id){		
		ModelAndView mav=new ModelAndView();
		mav.addObject("weiXiu", weiXiuManager.get(id));
		mav.addObject("depList", depManager.getAllDep());
		mav.setViewName("weixiu/weiXiuZD");
		return mav;
	}
	
	@RequestMapping(value = "/weiXiuZDWXR")
	public void weiXiuZDWXR(String id,String weiXiuRId,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		WeiXiu weiXiu=weiXiuManager.get(id);
		weiXiu.setWeiXiuR(userManager.getUser(weiXiuRId));
		weiXiu.setHuanJie(huanJieManager.get(403));
		weiXiuManager.cover(weiXiu);
		WeiXiuJL weiXiuJL=new WeiXiuJL();
		weiXiuJL.setCaoZuoRQ(new Date());
		weiXiuJL.setHuanJie(huanJieManager.get(402));
		weiXiuJL.setPiFuLX("通过");
		weiXiuJL.setPiFuNR("指定维修人");
		weiXiuJL.setUser(user);
		weiXiuJL.setWeiXiu(weiXiu);
		weiXiuJLManager.save(weiXiuJL);
		WuZi wuZi=weiXiu.getWuZi();
		wuZi.setShiYongZT(shiYongZTManager.get(103));
		wuZiManager.cover(wuZi);
		//指定维修人
		toDoManager.deleteList(weiXiu);
		ToDo toDo=new ToDo();
		toDo.setCreateDate(new Date());
		toDo.setHuanJie(huanJieManager.get(403));
		toDo.setTask("维修任务");
		toDo.setType("维修");
		toDo.setUser(weiXiu.getWeiXiuR());
		toDo.setWeiXiu(weiXiu);
		toDoManager.save(toDo);
		String root=rootManager.getRoot();
		Email email=emailManager.getEmail();
		//\\发送邮件
		YouJian youJian=youJianManager.get(403);
		if(youJian!=null && youJian.getState()==1){
			SendMailThread st=new SendMailThread(email, root, 403, weiXiu.getWeiXiuR(), youJian);
			st.setWeiXiu(weiXiu);
			st.setWuZi(wuZi);
			Thread t=new Thread(st);
			t.start();
		}
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/zdwxrPL")
	public ModelAndView zdwxrPL(HttpServletRequest request){
		ModelAndView mav=new ModelAndView();
		String[] type = request.getParameterValues("wxId");
		if(type!=null){
			String wxIds="";
			for(int i=0;i<type.length;i++){
				wxIds=wxIds+type[i]+",";
			}
			if(wxIds.length()>0){
				wxIds=wxIds.substring(0, wxIds.length()-1);
			}
			mav.addObject("wxIds", wxIds);
		}
		mav.addObject("depList", depManager.getAllDep());
		mav.setViewName("weixiu/zdwxrPL");
		return mav;
	}
	
	@RequestMapping(value = "/zdwxrPLSave")
	public void zdwxrPLSave(String wxIds,String weiXiuRId,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		String root=rootManager.getRoot();
		Email email=emailManager.getEmail();
		String wxId[]=wxIds.split(",");
		for(int i=0;i<wxId.length;i++){
			WeiXiu weiXiu=weiXiuManager.get(wxId[i]);
			weiXiu.setWeiXiuR(userManager.getUser(weiXiuRId));
			weiXiu.setHuanJie(huanJieManager.get(403));
			weiXiuManager.cover(weiXiu);
			WeiXiuJL weiXiuJL=new WeiXiuJL();
			weiXiuJL.setCaoZuoRQ(new Date());
			weiXiuJL.setHuanJie(huanJieManager.get(402));
			weiXiuJL.setPiFuLX("通过");
			weiXiuJL.setPiFuNR("指定维修人");
			weiXiuJL.setUser(user);
			weiXiuJL.setWeiXiu(weiXiu);
			weiXiuJLManager.save(weiXiuJL);
			WuZi wuZi=weiXiu.getWuZi();
			wuZi.setShiYongZT(shiYongZTManager.get(103));
			wuZiManager.cover(wuZi);
			//指定维修人
			toDoManager.deleteList(weiXiu);
			ToDo toDo=new ToDo();
			toDo.setCreateDate(new Date());
			toDo.setHuanJie(huanJieManager.get(403));
			toDo.setTask("维修任务");
			toDo.setType("维修");
			toDo.setUser(weiXiu.getWeiXiuR());
			toDo.setWeiXiu(weiXiu);
			toDoManager.save(toDo);
			//\\发送邮件
			YouJian youJian=youJianManager.get(403);
			if(youJian!=null && youJian.getState()==1){
				SendMailThread st=new SendMailThread(email, root, 403, weiXiu.getWeiXiuR(), youJian);
				st.setWeiXiu(weiXiu);
				st.setWuZi(wuZi);
				Thread t=new Thread(st);
				t.start();
			}
		}
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/wxspPL")
	public void wxspPL(HttpServletRequest request,HttpServletResponse response) {		
		String[] type = request.getParameterValues("wxId");
		String result="{\"result\":\"none\"}";
		if(type!=null){
			result="{\"result\":\"success\"}";
		}
		response.setContentType("application/json");
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/wxspPLBH")
	public ModelAndView wxspPLBH(HttpServletRequest request) {
		String[] type = request.getParameterValues("wxId");
		String wxIds="";
		for(int i=0;i<type.length;i++){
			wxIds=wxIds+type[i]+",";
		}
		if(wxIds.length()>0)
			wxIds=wxIds.substring(0, wxIds.length()-1);
		ModelAndView mav=new ModelAndView();
		mav.addObject("wxIds", wxIds);
		mav.setViewName("weixiu/weiXiuPLBH");
		return mav;
	}
	
	@RequestMapping(value = "/weiXiuPLBHSave")
	public void weiXiuPLBHSave(String wxIds,String boHuiYY,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		String wxId[]=wxIds.split(",");
		for(int i=0;i<wxId.length;i++){
			WeiXiu weiXiu=weiXiuManager.get(wxId[i]);
			int dangQianHuanJie=weiXiu.getHuanJie().getId();
			weiXiu.setHuanJie(huanJieManager.get(407));
			weiXiuManager.cover(weiXiu);
			WeiXiuJL weiXiuJL=new WeiXiuJL();
			weiXiuJL.setCaoZuoRQ(new Date());
			weiXiuJL.setHuanJie(huanJieManager.get(dangQianHuanJie));
			weiXiuJL.setWeiXiu(weiXiu);
			weiXiuJL.setPiFuLX("不同意维修");
			weiXiuJL.setPiFuNR(boHuiYY);
			weiXiuJL.setUser(user);
			weiXiuJLManager.save(weiXiuJL);
			WuZi wuZi=weiXiu.getWuZi();
			wuZi.setShiYongZT(shiYongZTManager.get(102));
			wuZiManager.cover(wuZi);
			toDoManager.deleteList(weiXiu);
		}
		String result="{\"result\":\"success\"}";
		response.setContentType("application/json");
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/*
	 * 维修人维修任务
	 */
	@RequestMapping(value = "/wxrwList")
	public ModelAndView wxrwList(){
		ModelAndView mav=new ModelAndView();
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		mav.addObject("weiXiuList", weiXiuManager.getMyWeiXiuRW(user));
		mav.setViewName("weixiu/wxrwListAjax");
		return mav;
	}
	
	@RequestMapping(value = "/weiXiuSP")
	public ModelAndView weiXiuSP(String id){		
		ModelAndView mav=new ModelAndView();
		mav.addObject("weiXiu", weiXiuManager.get(id));
		mav.setViewName("weixiu/weiXiuSP");
		return mav;
	}
	
	@RequestMapping(value = "/weiXiuPZ")
	public void weiXiuPZ(String id,double feiYong,String weiXiuSM,String weiXiuShang,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		WeiXiu weiXiu=weiXiuManager.get(id);
		weiXiu.setFeiYong(feiYong);
		weiXiu.setWeiXiuShang(weiXiuShang);
		weiXiu.setWeiXiuSM(weiXiuSM);
		weiXiu.setHuanJie(huanJieManager.get(404));
		weiXiuManager.cover(weiXiu);
		WeiXiuJL weiXiuJL=new WeiXiuJL();
		weiXiuJL.setCaoZuoRQ(new Date());
		weiXiuJL.setHuanJie(huanJieManager.get(403));
		weiXiuJL.setPiFuLX("通过");
		weiXiuJL.setPiFuNR("同意");
		weiXiuJL.setUser(user);
		weiXiuJL.setWeiXiu(weiXiu);
		weiXiuJLManager.save(weiXiuJL);
		User shiYongR = weiXiu.getWuZi().getUser();
		//使用人确认
		toDoManager.deleteList(weiXiu);
		ToDo toDo=new ToDo();
		toDo.setCreateDate(new Date());
		toDo.setHuanJie(huanJieManager.get(404));
		toDo.setTask("维修确认");
		toDo.setType("维修");
		toDo.setUser(shiYongR);
		toDo.setWeiXiu(weiXiu);
		toDoManager.save(toDo);
		String root=rootManager.getRoot();
		Email email=emailManager.getEmail();
		//\\发送邮件
		YouJian youJian=youJianManager.get(404);
		if(youJian!=null && youJian.getState()==1){
			SendMailThread st=new SendMailThread(email, root, 404, shiYongR, youJian);
			st.setWeiXiu(weiXiu);
			st.setWuZi(weiXiu.getWuZi());
			Thread t=new Thread(st);
			t.start();
		}
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/wxrwTH")
	public ModelAndView wxrwTH(String id){
		ModelAndView mav=new ModelAndView();
		mav.addObject("weiXiu", weiXiuManager.get(id));
		mav.setViewName("weixiu/wxrwTH");
		return mav;
	}
	
	@RequestMapping(value = "/wxrwTHSave")
	public void wxrwTHSave(String wxId,String tuiHuiYY,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		WeiXiu weiXiu=weiXiuManager.get(wxId);
		weiXiu.setHuanJie(huanJieManager.get(402));
		weiXiuManager.cover(weiXiu);
		WeiXiuJL weiXiuJL=new WeiXiuJL();
		weiXiuJL.setCaoZuoRQ(new Date());
		weiXiuJL.setHuanJie(huanJieManager.get(403));
		weiXiuJL.setPiFuLX("退回");
		weiXiuJL.setPiFuNR(tuiHuiYY);
		weiXiuJL.setUser(user);
		weiXiuJL.setWeiXiu(weiXiu);
		weiXiuJLManager.save(weiXiuJL);
		toDoManager.deleteList(weiXiu);
		Email email=emailManager.getEmail();
		String root=rootManager.getRoot();
		for(User user1:userManager.getGZGLY()){
			ToDo toDo=new ToDo();
			toDo.setCreateDate(new Date());
			toDo.setHuanJie(huanJieManager.get(402));
			toDo.setTask("维修审批");
			toDo.setType("维修");
			toDo.setUser(user1);
			toDo.setWeiXiu(weiXiu);
			toDoManager.save(toDo);
			//\\发送邮件
			YouJian youJian=youJianManager.get(402);
			if(youJian!=null && youJian.getState()==1){
				SendMailThread st=new SendMailThread(email, root, 402, user1, youJian);
				st.setWeiXiu(weiXiu);
				st.setWuZi(weiXiu.getWuZi());
				Thread t=new Thread(st);
				t.start();
			}
		}
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/ghwxrPL")
	public ModelAndView ghwxrPL(HttpServletRequest request){
		ModelAndView mav=new ModelAndView();
		String[] type = request.getParameterValues("wxrwId");
		if(type!=null){
			String wxIds="";
			for(int i=0;i<type.length;i++){
				wxIds=wxIds+type[i]+",";
			}
			if(wxIds.length()>0){
				wxIds=wxIds.substring(0, wxIds.length()-1);
			}
			mav.addObject("wxIds", wxIds);
		}
		mav.addObject("depList", depManager.getAllDep());
		mav.setViewName("weixiu/zdwxrPL");
		return mav;
	}
	
	@RequestMapping(value = "/wxrwPL")
	public void wxrwPL(HttpServletRequest request,HttpServletResponse response) {		
		String[] type = request.getParameterValues("wxrwId");
		String result="{\"result\":\"none\"}";
		if(type!=null){
			result="{\"result\":\"success\"}";
		}
		response.setContentType("application/json");
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/wxrwPLTH")
	public ModelAndView wxrwPLTH(HttpServletRequest request){
		ModelAndView mav=new ModelAndView();
		String[] type = request.getParameterValues("wxrwId");
		String wxIds="";
		for(int i=0;i<type.length;i++){
			wxIds=wxIds+type[i]+",";
		}
		wxIds=wxIds.substring(0, wxIds.length()-1);
		mav.addObject("wxIds", wxIds);
		mav.setViewName("weixiu/wxrwPLTH");
		return mav;
	}
	
	@RequestMapping(value = "/wxrwPLTHSave")
	public void wxrwPLTHSave(String wxIds,String tuiHuiYY,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		Email email=emailManager.getEmail();
		String root=rootManager.getRoot();
		String wxId[]=wxIds.split(",");
		for(int i=0;i<wxId.length;i++){
			WeiXiu weiXiu=weiXiuManager.get(wxId[i]);
			weiXiu.setHuanJie(huanJieManager.get(402));
			weiXiuManager.cover(weiXiu);
			WeiXiuJL weiXiuJL=new WeiXiuJL();
			weiXiuJL.setCaoZuoRQ(new Date());
			weiXiuJL.setHuanJie(huanJieManager.get(403));
			weiXiuJL.setPiFuLX("退回");
			weiXiuJL.setPiFuNR(tuiHuiYY);
			weiXiuJL.setUser(user);
			weiXiuJL.setWeiXiu(weiXiu);
			weiXiuJLManager.save(weiXiuJL);
			toDoManager.deleteList(weiXiu);
			for(User user1:userManager.getGZGLY()){
				ToDo toDo=new ToDo();
				toDo.setCreateDate(new Date());
				toDo.setHuanJie(huanJieManager.get(402));
				toDo.setTask("维修审批");
				toDo.setType("维修");
				toDo.setUser(user1);
				toDo.setWeiXiu(weiXiu);
				toDoManager.save(toDo);
				//\\发送邮件
				YouJian youJian=youJianManager.get(402);
				if(youJian!=null && youJian.getState()==1){
					SendMailThread st=new SendMailThread(email, root, 402, user1, youJian);
					st.setWeiXiu(weiXiu);
					st.setWuZi(weiXiu.getWuZi());
					Thread t=new Thread(st);
					t.start();
				}
			}
		}
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/wxrwPLBH")
	public ModelAndView wxrwPLBH(HttpServletRequest request){
		ModelAndView mav=new ModelAndView();
		String[] type = request.getParameterValues("wxrwId");
		String wxIds="";
		for(int i=0;i<type.length;i++)
			wxIds=wxIds+type[i]+",";
		if(wxIds.length()>0)
			wxIds=wxIds.substring(0, wxIds.length()-1);
		mav.addObject("wxIds", wxIds);
		mav.setViewName("weixiu/weiXiuPLBH");
		return mav;
	}
	
	/*
	 * 使用人确认
	 */
	@RequestMapping(value = "/wxqrList")
	public ModelAndView wxqrList(){
		ModelAndView mav=new ModelAndView();
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		mav.addObject("weiXiuLingYong", weiXiuManager.getMyWeiXiuQR(user));
		mav.setViewName("weixiu/wxqrListAjax");
		return mav;
	}
	
	@RequestMapping(value = "/weiXiuQueRen")
	public void weiXiuQueRen(String id,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		WeiXiu weiXiu=weiXiuManager.get(id);
		weiXiu.setHuanJie(huanJieManager.get(405));
		weiXiuManager.cover(weiXiu);		
		WeiXiuJL weiXiuJL=new WeiXiuJL();
		weiXiuJL.setCaoZuoRQ(new Date());
		weiXiuJL.setHuanJie(huanJieManager.get(404));
		weiXiuJL.setPiFuLX("使用人维修确认");
		weiXiuJL.setPiFuNR("同意");
		weiXiuJL.setUser(user);
		weiXiuJL.setWeiXiu(weiXiu);
		weiXiuJLManager.save(weiXiuJL);
		WuZi wuZi=weiXiu.getWuZi();
		wuZi.setShiYongZT(shiYongZTManager.get(102));
		wuZiManager.cover(wuZi);
		String root=rootManager.getRoot();
		Email email=emailManager.getEmail();
		//待维修入账
		toDoManager.deleteList(weiXiu);
		for(User user1:userManager.getGZGLY()){
			ToDo toDo=new ToDo();
			toDo.setCreateDate(new Date());
			toDo.setHuanJie(huanJieManager.get(405));
			toDo.setTask("维修入账");
			toDo.setType("维修");
			toDo.setUser(user1);
			toDo.setWeiXiu(weiXiu);
			toDoManager.save(toDo);
			//\\发送邮件
			YouJian youJian=youJianManager.get(405);
			if(youJian!=null && youJian.getState()==1){
				SendMailThread st=new SendMailThread(email, root, 405, user1, youJian);
				st.setWeiXiu(weiXiu);
				st.setWuZi(weiXiu.getWuZi());
				Thread t=new Thread(st);
				t.start();
			}
		}
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/wxqrPL")
	public void wxqrPL(HttpServletRequest request,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		String root=rootManager.getRoot();
		Email email=emailManager.getEmail();
		String type[]=request.getParameterValues("wxqrId");
		if(type!=null){
			for(int i=0;i<type.length;i++){
				WeiXiu weiXiu=weiXiuManager.get(type[i]);
				weiXiu.setHuanJie(huanJieManager.get(405));
				weiXiuManager.cover(weiXiu);		
				WeiXiuJL weiXiuJL=new WeiXiuJL();
				weiXiuJL.setCaoZuoRQ(new Date());
				weiXiuJL.setHuanJie(huanJieManager.get(404));
				weiXiuJL.setPiFuLX("使用人维修确认");
				weiXiuJL.setPiFuNR("同意");
				weiXiuJL.setUser(user);
				weiXiuJL.setWeiXiu(weiXiu);
				weiXiuJLManager.save(weiXiuJL);
				WuZi wuZi=weiXiu.getWuZi();
				wuZi.setShiYongZT(shiYongZTManager.get(102));
				wuZiManager.cover(wuZi);
				
				//待维修入账
				toDoManager.deleteList(weiXiu);
				for(User user1:userManager.getGZGLY()){
					ToDo toDo=new ToDo();
					toDo.setCreateDate(new Date());
					toDo.setHuanJie(huanJieManager.get(405));
					toDo.setTask("维修入账");
					toDo.setType("维修");
					toDo.setUser(user1);
					toDo.setWeiXiu(weiXiu);
					toDoManager.save(toDo);
					//\\发送邮件
					YouJian youJian=youJianManager.get(405);
					if(youJian!=null && youJian.getState()==1){
						SendMailThread st=new SendMailThread(email, root, 405, user1, youJian);
						st.setWeiXiu(weiXiu);
						st.setWuZi(weiXiu.getWuZi());
						Thread t=new Thread(st);
						t.start();
					}
				}
			}
		}
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/fangHuiKuCun")
	public void fangHuiKuCun(String id,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		WeiXiu weiXiu=weiXiuManager.get(id);
		weiXiu.setHuanJie(huanJieManager.get(405));
		weiXiuManager.cover(weiXiu);
		WeiXiuJL weiXiuJL=new WeiXiuJL();
		weiXiuJL.setCaoZuoRQ(new Date());
		weiXiuJL.setHuanJie(huanJieManager.get(404));
		weiXiuJL.setPiFuLX("使用人维修确认");
		weiXiuJL.setPiFuNR("同意");
		weiXiuJL.setUser(user);
		weiXiuJL.setWeiXiu(weiXiu);
		weiXiuJLManager.save(weiXiuJL);
		WuZi wuZi=weiXiu.getWuZi();
		wuZi.setShiYongZT(shiYongZTManager.get(105));
		wuZiManager.cover(wuZi);
		Email email=emailManager.getEmail();
		String root=rootManager.getRoot();
		//待维修入账
		toDoManager.deleteList(weiXiu);
		for (User user1 : userManager.getGZGLY()) {
			ToDo toDo = new ToDo();
			toDo.setCreateDate(new Date());
			toDo.setHuanJie(huanJieManager.get(405));
			toDo.setTask("维修入账");
			toDo.setType("维修");
			toDo.setUser(user1);
			toDo.setWeiXiu(weiXiu);
			toDoManager.save(toDo);
			//\\发送邮件
			YouJian youJian=youJianManager.get(405);
			if(youJian!=null && youJian.getState()==1){
				SendMailThread st=new SendMailThread(email, root, 405, user1, youJian);
				st.setWeiXiu(weiXiu);
				st.setWuZi(weiXiu.getWuZi());
				Thread t=new Thread(st);
				t.start();
			}
		}
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/weiXiuBTY")
	public ModelAndView weiXiuBTY(String id){
		ModelAndView mav=new ModelAndView();		
		mav.addObject("weiXiuId", id);
		mav.setViewName("weixiu/weiXiuBTY");
		return mav;
	}
	
	@RequestMapping(value = "/weiXiuBTYSave")
	public void weiXiuBTYSave(String id,String piFuNR,HttpServletResponse response) {
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		WeiXiu weiXiu=weiXiuManager.get(id);
		int dangQianHuanJie=weiXiu.getHuanJie().getId();
		weiXiu.setHuanJie(huanJieManager.get(407));
		weiXiuManager.cover(weiXiu);
		WeiXiuJL weiXiuJL=new WeiXiuJL();
		weiXiuJL.setCaoZuoRQ(new Date());
		weiXiuJL.setHuanJie(huanJieManager.get(dangQianHuanJie));
		weiXiuJL.setWeiXiu(weiXiu);
		weiXiuJL.setPiFuLX("不同意维修");
		weiXiuJL.setPiFuNR(piFuNR);
		weiXiuJL.setUser(user);
		weiXiuJLManager.save(weiXiuJL);
		WuZi wuZi=weiXiu.getWuZi();
		wuZi.setShiYongZT(shiYongZTManager.get(102));
		wuZiManager.cover(wuZi);
		toDoManager.deleteList(weiXiu);
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/zaiCiWeiXiu")
	public ModelAndView zaiCiWeiXiu(String id){
		ModelAndView mav=new ModelAndView();		
		mav.addObject("weiXiuId", id);
		mav.setViewName("weixiu/zaiCiWeiXiu");
		return mav;
	}
	
	@RequestMapping(value = "/zaiCiWeiXiuSave")
	public void zaiCiWeiXiuSave(String id,String piFuNR,HttpServletResponse response) {
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		WeiXiu weiXiu=weiXiuManager.get(id);
		int dangQianHuanJie=weiXiu.getHuanJie().getId();
		weiXiu.setHuanJie(huanJieManager.get(402));
		weiXiuManager.cover(weiXiu);
		WeiXiuJL weiXiuJL=new WeiXiuJL();
		weiXiuJL.setCaoZuoRQ(new Date());
		weiXiuJL.setHuanJie(huanJieManager.get(dangQianHuanJie));
		weiXiuJL.setWeiXiu(weiXiu);
		weiXiuJL.setPiFuLX("再次维修");
		weiXiuJL.setPiFuNR(piFuNR);
		weiXiuJL.setUser(user);
		weiXiuJLManager.save(weiXiuJL);
		Email email=emailManager.getEmail();
		String root=rootManager.getRoot();
		//待维修入账
		toDoManager.deleteList(weiXiu);
		for (User user1 : userManager.getGZGLY()) {
			ToDo toDo = new ToDo();
			toDo.setCreateDate(new Date());
			toDo.setHuanJie(huanJieManager.get(402));
			toDo.setTask("维修审批");
			toDo.setType("维修");
			toDo.setUser(user1);
			toDo.setWeiXiu(weiXiu);
			toDoManager.save(toDo);
			//\\发送邮件
			YouJian youJian=youJianManager.get(402);
			if(youJian!=null && youJian.getState()==1){
				SendMailThread st=new SendMailThread(email, root, 402, user1, youJian);
				st.setWeiXiu(weiXiu);
				st.setWuZi(weiXiu.getWuZi());
				Thread t=new Thread(st);
				t.start();
			}
		}
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/*
	 * 低值管理员维修入账
	 */
	@RequestMapping(value = "/wxrzList")
	public ModelAndView wxrzList(){
		ModelAndView mav=new ModelAndView();
		mav.addObject("weiXiuQueRenList", weiXiuManager.getAllWeiXiuByZT(405));
		mav.setViewName("weixiu/wxrzListAjax");
		return mav;
	}
	
	@RequestMapping(value = "/weiXiuRZ")
	public void weiXiuRZ(String id,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		WeiXiu weiXiu=weiXiuManager.get(id);
		weiXiu.setHuanJie(huanJieManager.get(406));
		weiXiuManager.cover(weiXiu);		
		WeiXiuJL weiXiuJL=new WeiXiuJL();
		weiXiuJL.setCaoZuoRQ(new Date());
		weiXiuJL.setHuanJie(huanJieManager.get(405));
		weiXiuJL.setPiFuLX("维修入账");
		weiXiuJL.setPiFuNR("入账");
		weiXiuJL.setUser(user);
		weiXiuJL.setWeiXiu(weiXiu);
		weiXiuJLManager.save(weiXiuJL);
		toDoManager.deleteList(weiXiu);
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/wxrzPiSubmit")
	public void wxrzPiSubmit(HttpServletRequest request,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		String[] type = request.getParameterValues("wxrzId");
		for(int i=0;i<type.length;i++){
			WeiXiu weiXiu=weiXiuManager.get(type[i]);
			weiXiu.setHuanJie(huanJieManager.get(406));
			weiXiuManager.cover(weiXiu);		
			WeiXiuJL weiXiuJL=new WeiXiuJL();
			weiXiuJL.setCaoZuoRQ(new Date());
			weiXiuJL.setHuanJie(huanJieManager.get(405));
			weiXiuJL.setPiFuLX("维修入账");
			weiXiuJL.setPiFuNR("入账");
			weiXiuJL.setUser(user);
			weiXiuJL.setWeiXiu(weiXiu);
			weiXiuJLManager.save(weiXiuJL);
			toDoManager.deleteList(weiXiu);
		}
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/weiXiuDetail")
	public ModelAndView weiXiuDetail(String id){
		WeiXiu weiXiu=weiXiuManager.get(id);
		ModelAndView mav=new ModelAndView();
		mav.addObject("weiXiu", weiXiu);
		mav.addObject("xiaYiBuR", getXiaYiBuR(id));
		mav.addObject("weiXiuJLList", weiXiuJLManager.getAllJL(id));
		mav.setViewName("weixiu/weiXiuDetail");
		return mav;
	}
	
	@RequestMapping(value = "/myWeiXiuSP")
	public ModelAndView myWeiXiuSP(){
		ModelAndView mav=new ModelAndView();
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		mav.addObject("weiXiuSPList", weiXiuJLManager.getMySPJL(user));
		mav.setViewName("weixiu/myWeiXiuSP");
		return mav;
	}
}
